﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YmtAuth.AppService;
using YmtAuth.Domain.Repository.EventRepository;
using YmtSystem.CrossCutting;

namespace YmtUserAuth.DataImport2
{
    public class DataImportUserBillupEvent
    {
        public static void Import()
        {
            try
            {
                var filePath = System.Configuration.ConfigurationManager.AppSettings["data2"];
                var fullPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data" + "\\" + filePath);
                Console.WriteLine("执行导入数据");
                var watch = Stopwatch.StartNew();
                var fields = ReadFile.SyncRead(fullPath);
                var data = FieldToUserBills.AdapterUserBillsUpEvent(fields);
                Console.WriteLine("数据转换完毕 共 {0} 行， 耗时 {1} 秒 ", data.Count(), watch.Elapsed.TotalSeconds);
                Console.WriteLine("程序启动");
                YmatouFramework.Start();
                Console.WriteLine("程序启动完成，开始导入");
                var server = LocalServiceLocator.GetService<ILoginIdUpdateEventRepository>();
                var mongo = LocalServiceLocator.GetService<YmtAuth.Repository.Mongodb.MongoConext>();
                var page = data.Count() / 1000 + (data.Count() % 1000 > 0 ? 1 : 0);
                Console.WriteLine("page {0}", page);
                Console.WriteLine("按任意键开始导入");
                Console.Read();
                for (var i = 0; i < page; i++)
                {
                    var datas = data.Skip(i * 1000).Take(1000);
                    server.AddRang(datas, "Ymt_UserBills_Event", "LoginIdUpdateEventHistory");
                }
                Console.WriteLine("完成导入 ，耗时 {0} 秒", watch.Elapsed.TotalSeconds);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.Read();
            YmatouFramework.Stop();
        }
    }
}
